% Mengzi Zhang
% CIS 520 hw 6
%

function K = kernel_gaussian(X, X2, sigma)
% Evaluates the Gaussian Kernel with specified sigma
%
% Usage:
%
%    K = KERNEL_GAUSSIAN(X, X2, SIGMA)
%
% For a N x D matrix X and a M x D matrix X2, computes a M x N kernel
% matrix K where K(i,j) = k(X(i,:), X2(j,:)) and k is the Guassian kernel
% with parameter sigma=20.

n = size(X,1);
m = size(X2,1);
K = zeros(m, n);

% HINT: Transpose the sparse data matrix X, so that you can operate over columns. Sparse
% column operations in matlab are MUCH faster than row operations.

% YOUR CODE GOES HERE.

% D x n
X_t = X';
% D x m
X2_t = X2';

for i = 1 : m
  
  % D x 1
  a = X2_t (:, i);
  
  % D x n, = a - all cols in X_t
  diff = bsxfun (@minus, a, X_t);
  
  % l2 norm
  % square the difference. D x n
  square = bsxfun (@times, diff, diff);

  % sum the squares. 1 x n
  square_sum = sum (square, 1);

  % (here, instead of taking sqrt to get l2norm, then square again, don't have
  %   to do anything. Same thing.)
  
  % divide square-sum by 2 * sigma^2. 1 x n
  quotient = square_sum ./ (2 * sigma^2);
  
  % answer for k(x, x'). similarity measure. 1 x n
  sim = exp (-quotient);
  
  % save answer into kernal mat K
  K (i, :) = sim;
  
  
%   a = X2 (i, :);
% 
%   for j = 1 : n
%     b = X_t (:, j);
%     b = b';
%     
%     K (i, j) = exp ((norm (a - b, 2)) .^ 2) / (2 * sigma^2));
%   end
end


